<template>
  <page-header-wrapper>
    <global-search :loading="searchLoading" :list="searchList" @search="handleSearch" @reset="handleReset">
    </global-search>
    <a-card :bordered="false">
      <s-table
        ref="table"
        size="default"
        rowKey="ID"
        :columns="columns"
        :data="loadData"
        :scroll="{ x: true }"
        :pagination="{ showTotal: (total) => `共 ${total} 条` }">
        <span slot="index" slot-scope="text, record, index">{{ index + 1 }} </span>
        <span slot="time" slot-scope="text">{{ text | moment }}</span>
        <ellipsis :length="46" slot="ellipsis" slot-scope="text">{{ text }}</ellipsis>
        <div slot="action" slot-scope="text, record">
          <y-button type="link" @click="handleAction(record, 'detail')">查看</y-button>
        </div>
      </s-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import { getFollowUpList } from '@/api/followUpManagement'
import { getHouseTypeColor } from '@/api/common'
export default {
  components: {
    STable,
    Ellipsis
  },
  data () {
    return {
      searchLoading: false,
      searchList: [
        {
          label: '申请人',
          fields: 'Proposer',
          type: 'input',
          props: {
            placeholder: '请输入申请人'
          }
        },
        {
          label: '身份证号',
          fields: 'IDcard',
          type: 'input',
          props: {
            placeholder: '请输入身份证号'
          }
        },
        {
          label: '申请编号',
          fields: 'ApplyNO',
          type: 'input',
          props: {
            placeholder: '请输入申请编号'
          }
        },
        {
          label: '房屋状态',
          fields: '状态',
          type: 'select',
          props: {
            placeholder: '请选择房屋状态',
            options: []
          }
        },
        {
          label: '项目名称',
          fields: '项目',
          type: 'input',
          props: {
            placeholder: '请输入项目名称'
          }
        },
        {
          label: '房屋信息',
          fields: 'ESitesSelect',
          type: 'input',
          props: {
            placeholder: '请输入房屋信息'
          }
        }
      ],
      queryParam: {
        Mode: 'AlterHousesProposer',
        UserID: this.$store.getters.userid,
        strContent: {},
        DeptNO: this.$store.getters.userInfo.DeptNO
      },
      columns: [
        {
          title: '序号',
          align: 'center',
          width: '80px',
          scopedSlots: { customRender: 'index' }
        },
        {
          title: '申请编号',
          align: 'center',
          dataIndex: 'ApplyNO'
        },
        {
          title: '姓名',
          align: 'center',
          dataIndex: 'Proposer'
        },
        {
          title: '身份证',
          align: 'center',
          dataIndex: 'Idcard'
        },
        {
          title: '分配时间',
          align: 'center',
          dataIndex: 'ESetDate',
          scopedSlots: { customRender: 'time' }
        },
        {
          title: '房屋地址',
          align: 'center',
          dataIndex: 'ESitesSelect',
          scopedSlots: { customRender: 'ellipsis' }
        },
        {
          title: '房屋状态',
          align: 'center',
          dataIndex: 'HousesState',
          customRender: (text) => {
             const houseType = this.searchList[3].props.options.find((e) => e.value === text)
            return <span style={{ color: houseType ? houseType.color : 'rgba(0, 0, 0, 0.65)' }}>{text}</span>
          }
        },
        {
          title: '合同编号',
          align: 'center',
          dataIndex: 'PactNo'
        },
        {
          title: '项目名称',
          align: 'center',
          dataIndex: 'ProjectName'
        },
        {
          title: '异动时间',
          align: 'center',
          dataIndex: 'AlterDate',
          scopedSlots: { customRender: 'time' }
        },
        {
          title: '异动原因',
          align: 'center',
          dataIndex: 'AlterInfo',
          scopedSlots: { customRender: 'ellipsis' }
        },
        {
          title: '操作',
          align: 'center',
          width: 80,
          fixed: 'right',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return getFollowUpList(requestParameters).then((res) => res)
      }
    }
  },
  created () {
    this.fetchData()
  },
  methods: {
    fetchData () {
      this.searchLoading = true
      getHouseTypeColor()
        .then((res) => {
          this.searchList[3].props.options = res.map((e) => {
            return {
              value: e.PMNAME,
              label: `${e.PMNAME}${e.PMBREAK ? `(${e.PMBREAK})` : ''}`,
              color: e.PMCOLOR
            }
          })
        })
        .finally(() => {
          this.searchLoading = false
        })
    },
    handleSearch (values) {
      this.queryParam.strContent = values
      this.tableRefresh(true)
    },
    handleReset () {
      this.queryParam.strContent = {}
      this.tableRefresh(true)
    },
    tableRefresh (refresh = false) {
      this.$refs.table.refresh(refresh)
    },
    handleAction (record, type) {
      if (type === 'detail') {
        this.$router.push({
          name: 'followUpManagementLesseeChangeDetail',
          params: {
            id: record.ID
          }
        })
      }
    }
  }
}
</script>
<style lang="less" scoped>
/deep/ .ant-table td {
  white-space: nowrap;
}
</style>
